package com.zabra.zl.ui.addbank;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseActivity;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.BankData;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.addbank.contract.AddBankContract;
import com.zabra.zl.ui.addbank.presenter.AddBankPresenter;
import com.zabra.zl.ui.deposit.DepositActivity;
import com.zabra.zl.ui.inputpwd.InputPwdActivity;
import com.zabra.zl.ui.mybank.MyBankActivity;
import com.zabra.zl.ui.personal_center.PersonalActivity;
import com.zabra.zl.ui.success.UnbundleBankSuccessActivity;
import com.zabra.zl.ui.verifybank.VerifyBankActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.HideUtil;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import butterknife.OnTextChanged;

public class AddBankActivity extends BaseMvpActivity<AddBankPresenter> implements AddBankContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_idcard)
    TextView tvIdcard;
    @BindView(R.id.et_bankid)
    EditText etBankid;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.rl_chooseaddress)
    RelativeLayout rlChooseaddress;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private String strIdcard, strMessage, isdeal;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bank;
    }

    @Override
    public void initView() {
        tvTitle.setText("添加银行卡");
        mPresenter = new AddBankPresenter();
        mPresenter.attachView(this);

        String username = SharedPreferencesUtil.getInstance(this).getSP("username");
        String idcard = SharedPreferencesUtil.getInstance(this).getSP("idcard");
        isdeal = getIntent().getExtras().getString("isdeal");
        tvName.setText(username);
        tvIdcard.setText(HideUtil.hideCardNo(idcard));
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @OnTextChanged(R.id.et_bankid)
    public void onTextChanged(CharSequence text) {
        //内容改变监听
        strIdcard = text.toString();
        int length = strIdcard.length();
        if (length > 15) {
            keyList.add("cardNo");
            valueList.add(strIdcard);
            mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
            keyList.clear();
            valueList.clear();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onReceiveMsg(EventMessage message) {
        strMessage = message.getMessage();
    }

    @OnClick({R.id.back, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_next:
                if (TextUtils.isEmpty(tvBank.getText().toString().trim())) {
                    ToastUtil.showToast(this, "请输入正确银行卡号");
                } else {
                    keyList.add("cardNo");
                    keyList.add("debtorId");
                    valueList.add(strIdcard);
                    valueList.add(strUserid);
                    mPresenter.getBindData(MaptoJsonUtils.Simple(keyList, valueList));
                }

                break;
        }
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(BankData bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            tvBank.setText(bean.getBankName());
            SharedPreferencesUtil.getInstance(this).putSP("token", token);
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            tvBank.setText("");
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            if (isdeal.equals("no")) {
                //第一次添加，需要设置交易密码
                toClass1(this, InputPwdActivity.class);
            } else {
                //不是第一次添加，不需要设置
                if (strMessage.equals("MyBank") || strMessage.equals("Person")) {
                    //跳转到银行卡界面
                    toClass_EmptyTop(this, PersonalActivity.class);
                }
                if (strMessage.equals("Deposit")) {
                    //跳转到提现界面
                    toClass_EmptyTop(this, DepositActivity.class);
                }
            }

        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
